<!DOCTYPE html>
<html>
<head>
    <title>添加</title>
	<include file='public/head'>
</head>
<body>
	<!-- 内容开始 -->
	<div class="layui-tab">
		<!-- 表单开始 -->
		<form method="POST" class="layui-form" id="da" action="" name="form" >

			<input type="hidden" name="add[id]" >

			<div class="layui-form-item">
				<label class="layui-form-label">输入框</label>
				<div class="layui-input-inline" >
					<input type="text" name="add[title]" required  lay-verify="title" placeholder="请输入标题" autocomplete="off" class="layui-input" >
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">密码框</label>
				<div class="layui-input-inline">
					<input type="password" name="add[password]" required  lay-verify="pass"  placeholder="请输入密码" autocomplete="off" class="layui-input" >
				</div>
				<div class="layui-form-mid layui-word-aux">辅助文字</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">城市</label>
				<div class="layui-input-inline">
					<select name="add[city]" lay-verify="required">
						<option value="">请选择...</option>
						<option value="1">杭州</option>
						<option value="2">台州</option>
					</select>
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">复选框</label>
				<div class="layui-input-block">
					<input type="checkbox" name="add[likes][write]" title="写作">
					<input type="checkbox" name="add[likes][read]" title="阅读" checked>
					<input type="checkbox" name="add[likes][dai]" title="发呆">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">开关</label>
				<div class="layui-input-block">
					<input type="checkbox" name="add[switch]" lay-skin="switch" ay-text="开启|关闭">
				</div>
				<div class="layui-input-block">
					<button type="button" class="layui-btn" id="test1">上传图片</button>
					<img class="layui-upload-img" id="demo1" width="150px"  >
					<p id="demoText"></p>
				</div>   
			</div>
			
			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label">文本域</label>
				<div class="layui-input-block">
					<textarea name="add[content]" placeholder="请输入内容" lay-verify="content" class="layui-textarea"></textarea>
				</div>
			</div>
			
		</form>
		<!-- 表单结束 -->
    </div>
	<!-- 内容结束 -->
	<include file='public/scripts'>

	<script>
	;!function(){
		var form = layui.form
			,layer = layui.layer    //弹出层必需
			,layedit = layui.layedit  //富文本必需
			,laydate = layui.laydate  //日期选择必需
			,table = layui.table  //表格必需
			,element = layui.element  //选项卡
			,upload = layui.upload;
	}();


	upload = layui.upload;
	//普通图片上传
	var uploadInst = upload.render({
		elem: '#test1'
		,url: 'https://httpbin.org/post' //改成您自己的上传接口
		,before: function(obj){
			//预读本地文件示例，不支持ie8
			obj.preview(function(index, file, result){
			$('#demo1').attr('src', result); //图片链接（base64）
			});
		}
		,done: function(res){
			//如果上传失败
			if(res.code != 200){
			return layer.msg('上传失败');
			}
			//上传成功
		}
		,error: function(){
			//演示失败状态，并实现重传
			var demoText = $('#demoText');
			demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
			demoText.find('.demo-reload').on('click', function(){
			uploadInst.upload();
			});
		}
	});
	

	/**
	 * 子页面方法
	 */
	function callbackdata(index){
		var dara = $("#da").serialize();
		let add_url = "{:cmf_plugin_url('Demo://admin_index/add')}";

		//ajax请求
		$.ajax({
				url:add_url,
				type:'post',
				data:dara,
				success:function(data){
					console.log(data);
					let icon = ( data.code == 200 )  ? 1 : 5 ; 
					layer.msg( data.message , { time:1200 , icon:icon },function(){
						if(icon = 1){
							parent.layer.close(index);//关闭弹框
							window.parent.location.reload(index);//刷新父页
						}	
					});
				},
				error:function(data){
					console.log(data);
					layer.msg('操作失败');
					return false;
				}
		})
	}    
	</script>

</body>
</html>

